<?php
$this->useClass("Action_Community_FeedHandler");
class Action_Community_ShowComMembers extends Action_Community_FeedHandler
{
	public function getFeedModel() {
		$this->rh->UseClass("AggregatedModel_Feed_FeedInterface");
		$this->rh->UseClass("AggregatedModel_Feed_ReaderUsers");

		return new AggregatedModel_Feed_ReaderUsers();
	}

	private function renderMenu ()
	{
		$menu = array(
			array(
				'action' => 'name',
				'title'  => 'Order by name',
			),
			/*
			array(
				'action' => 'date',
				'title'  => 'Order by regdate',
			),
			*/
			/*
			array(
				'action' => 'activity',
				'title'  => 'Order by activity',
			),
			 */
		);
		return $menu;
	} // end of function renderMenu

	private function getRoles() {
		$roles_ql = 'select objects where role.oid == 9 OR role.oid == 12'; // показываем только часть ролей #196
		$rs = $this->ctx->store->find($roles_ql);
		if ($rs) {
			$roles = array();
			while ($role = $rs->next()) {
				$t = $role->data;
				$t['oid'] = $role->id;
				$roles[$role->id] = $t;
			}
		}
		return $roles;
	}

	public function determineCustomModelParams() {
		if ($this->rh->GET['o_type'] == "desc") {
			$this->setParam('order_direction', "desc");
		} else {
			$this->setParam('order_direction', "asc");
		}
	}

	public function process ()
	{
		$aa = &$this->aa;

		$view = $this->rh->views->findByName('users');
		$view->data = $this->aa->template_data;
//		$this->setParam("order_by", "name");
//		$this->setParam("order_direction", "desc");
		$this->model->doStuff();

		$view->data['order_direction'] = $this->getParam("order_direction");
		$view->data['order_by'] = $this->getParam("order_by");

		$view->data['items'] = $this->model->getItems();
		$view->data['actions'] = $aa->getAvailableOperations();
		// HACK : 2008-04-24 : lucky
		//
		// $view->data['menu'] -- это специальная переменная для главного меню, которая содержит
		// список разделов
		//$view->data['menu'] = $this->renderMenu();
		$view->data['roles'] = $this->getRoles();

		// данные для постраничной листалки
		$pager = $this->getPagerInfo();

		$view->data['page'] = $pager;

		// генерация урл адресов
		$this->rh->UseClass("RL_URLGenerator");
		$ug = new RL_URLGenerator($this->rh->router->getLastMatched());
		$ug->setRouter($this->rh->router);
		$ug->setParams($this->params);

		// урл для постраничной листалки без ajax
		if (isset($pager['prev'])) {
			$ug->setGet(array_merge($this->rh->GET, array("p"=>$pager['prev'])));
			$view->data["prev_url"] = $ug->generateUrl();
		}

		if (isset($pager['next'])) {
			$ug->setGet(array_merge($this->rh->GET, array("p"=>$pager['next'])));
			$view->data["next_url"] = $ug->generateUrl();
		}

		$odir = $this->getParam("order_direction");
		$order_by = $this->getParam("order_by");

		// эта штука генерирует ссылки для трех ссылок для сортировки по разным полям
		$order_types = array("name", "status", "date");

		foreach ($order_types as $otype) {
			if ($order_by == $otype) {
				$params = array(
					"sort"=>$otype,
					"o_type"=>($odir=="desc")?"asc":"desc",
				);
			} else {
				$params = array("sort"=>$otype);
			}
			$ug->setGet(array_merge($this->rh->GET, $params));
			$view->data["sort_url_".$otype] = $ug->generateUrl();
		}

		unset($ug);

		$view->data['__widgets__'] = $this->widgets;

		return $this->rh->response->setContent( $view );
	} // end of function execute

	public function getWidgetsConfig ()
	{
		$widgets = array();

		$widgets[] = array (
			'name' => 'RssWidget',
			'params' => array(
				'widget_view' => 'WG_RssWidget',
			),
		);

		$widgets[] = array (
			'name' => 'CreateBtnWidget',
			'params' => array(
				'widget_position' => 'createbtn',
				'widget_view' => 'WG_CreateBtnWidget',
			),
		);

		return $widgets;
	} // end of function getWidgetsConfig

}
